package q1.project.log;

import jakarta.persistence.*;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
import q1.project.core.jpa.annotation.Comment;

/**
 * 操作日志实体
 *
 * @author Q1 Team
 * @version 1.0
 */
@Getter
@Setter
@Entity
@Table(
    name = "q1_operation_log",
    indexes = {
      @Index(name = "idx_tenant_id", columnList = "tenant_id"),
      @Index(name = "idx_user_id", columnList = "user_id"),
      @Index(name = "idx_module", columnList = "module"),
      @Index(name = "idx_create_time", columnList = "create_time")
    })
public class OperationLogEntity {

  @Comment("主键ID")
  @Id
  @Column(length = 32)
  private String id;

  /** 租户ID */
  @Comment("租户ID")
  @Column(name = "tenant_id", length = 32)
  private String tenantId;

  /** 操作用户ID */
  @Comment("操作用户ID")
  @Column(name = "user_id", length = 32)
  private String userId;

  /** 操作用户名 */
  @Comment("操作用户名")
  @Column(name = "username", length = 50)
  private String username;

  /** 操作模块 */
  @Comment("操作模块")
  @Column(name = "module", length = 50)
  private String module;

  /** 操作类型 */
  @Comment("操作类型")
  @Column(name = "type", length = 20)
  private String type;

  /** 操作描述 */
  @Comment("操作描述")
  @Column(name = "description", length = 500)
  private String description;

  /** 请求方法 */
  @Comment("请求方法")
  @Column(name = "method", length = 200)
  private String method;

  /** 请求URL */
  @Comment("请求URL")
  @Column(name = "url", length = 500)
  private String url;

  /** 请求参数 */
  @Comment("请求参数")
  @Column(name = "params", columnDefinition = "TEXT")
  private String params;

  /** 响应结果 */
  @Comment("响应结果")
  @Column(name = "result", columnDefinition = "TEXT")
  private String result;

  /** 执行时长（毫秒） */
  @Comment("执行时长（毫秒）")
  @Column(name = "execution_time")
  private Long executionTime;

  /** 是否成功 */
  @Comment("是否成功")
  @Column(name = "success")
  private Boolean success;

  /** 错误信息 */
  @Comment("错误信息")
  @Column(name = "error_msg", length = 2000)
  private String errorMsg;

  /** IP地址 */
  @Comment("IP地址")
  @Column(name = "ip", length = 50)
  private String ip;

  /** 浏览器 */
  @Comment("浏览器")
  @Column(name = "browser", length = 500)
  private String browser;

  /** 操作系统 */
  @Comment("操作系统")
  @Column(name = "os", length = 500)
  private String os;

  /** 创建时间 */
  @Comment("创建时间")
  @Column(name = "create_time", nullable = false, updatable = false)
  private LocalDateTime createTime;

  @PrePersist
  protected void onCreate() {
    if (id == null || id.isEmpty()) {
      id = java.util.UUID.randomUUID().toString().replace("-", "");
    }
    if (createTime == null) {
      createTime = LocalDateTime.now();
    }
  }
}
